# @Time : 2021/8/5 10:02
# @Author : Li Kunlun
# @Description : 填充和步幅

# 1、填充
from mxnet import nd
from mxnet.gluon import nn


# 定义一个函数来计算卷积层。它初始化卷积层权重，并对输入和输出做相应的升维和降维
def comp_conv2d(conv2d, X):
    conv2d.initialize()
    # (1, 1)代表批量大小和通道数均为1
    X = X.reshape((1, 1) + X.shape)
    Y = conv2d(X)
    return Y.reshape(Y.shape[2:])  # 排除不关心的前两维：批量和通道


# 注意这里是两侧分别填充1行或列，所以在两侧一共填充2行或列
conv2d = nn.Conv2D(1, kernel_size=3, padding=1)
X = nd.random.uniform(shape=(8, 8))
# (8, 8)
print(comp_conv2d(conv2d, X).shape)

# 2、步幅

# 高和宽上的步幅均为2，从而使输入的高和宽减半
conv2d = nn.Conv2D(1, kernel_size=3, padding=1, strides=2)
# (4, 4)
print(comp_conv2d(conv2d, X).shape)

conv2d = nn.Conv2D(1, kernel_size=(3, 5), padding=(0, 1), strides=(3, 4))
# (2, 2)
print(comp_conv2d(conv2d, X).shape)
